#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2019
# @Author  : linhu (hlf0312@gmail.com)
# @Link    : TBD
# @Version : $Id$

from openpyxl import Workbook
from openpyxl import load_workbook
from time import strftime, localtime
from JiraIssueCommon import buildJiraIssues, JiraIssue
from FeatureBuildsSchedule import getCurrentFB

def getIssueByKey(key, jiraIssues):
	for jiraIssue in jiraIssues:
		if jiraIssue.key == key:
			return jiraIssue
	return 'Null'

fbEntryFile = '1902_Entry_20190210225837.xlsx'
fbExitFile = '1902_Exit_20190212215257.xlsx'
genTime = strftime("%Y%m%d%H%M%S", localtime())
fbCountdownFile = 'fbCountdown' + genTime + '.xlsx'

fbEntryJiraIssues = buildJiraIssues(fbEntryFile)
fbExitJiraIssues = buildJiraIssues(fbExitFile)

jiraIssuesInFBEntry = []
jiraIssuesAfterFBEntry = []
jiraIssuesDeletedAfterFBEntry = []

wb = Workbook()
defaultWorksheet = wb.active
wb.remove(defaultWorksheet)
committedInFBEntryWs = wb.create_sheet('CommittedInFBEntry')
committedAfterFBEntryWs = wb.create_sheet('CommittedAfterFBEntry')
deletedAfterFBEntryWs = wb.create_sheet('DeletedAfterFBEntry')

for exitJiraIssue in fbExitJiraIssues:
	entryJiraIssue = getIssueByKey(exitJiraIssue.key, fbEntryJiraIssues)
	# print(entryJiraIssue)
	# print(exitJiraIssue)
	if entryJiraIssue != 'Null':
		updatedEntryJiraIssue = JiraIssue(entryJiraIssue.itemId, entryJiraIssue.key, entryJiraIssue.summary, \
			entryJiraIssue.startFb, entryJiraIssue.endFb, entryJiraIssue.team, \
			entryJiraIssue.fbCommitStatus, entryJiraIssue.estimated, entryJiraIssue.timeSpent, \
			entryJiraIssue.remainEstimate, exitJiraIssue.status, \
			entryJiraIssue.assignee, entryJiraIssue.stretchReason, entryJiraIssue.delayReason, \
			entryJiraIssue.delayExplanation, entryJiraIssue.competenceArea, \
			entryJiraIssue.nokiaPrio, entryJiraIssue.businessPrio1, entryJiraIssue.plannedSystemRelease, \
			entryJiraIssue.targetPT, entryJiraIssue.reporter, entryJiraIssue.tribeLeader, \
			entryJiraIssue.featureTeam, entryJiraIssue.entryEE, entryJiraIssue.problematicReasons, \
			exitJiraIssue.remainEstimate, (entryJiraIssue.entryEE - exitJiraIssue.remainEstimate))
		jiraIssuesInFBEntry.append(updatedEntryJiraIssue)
	else:
		updatedExitJiraIssue = JiraIssue(exitJiraIssue.itemId, exitJiraIssue.key, exitJiraIssue.summary, \
			exitJiraIssue.startFb, exitJiraIssue.endFb, exitJiraIssue.team, \
			exitJiraIssue.fbCommitStatus, exitJiraIssue.estimated, exitJiraIssue.timeSpent, \
			exitJiraIssue.remainEstimate, exitJiraIssue.status, \
			exitJiraIssue.assignee, exitJiraIssue.stretchReason, exitJiraIssue.delayReason, \
			exitJiraIssue.delayExplanation, exitJiraIssue.competenceArea, \
			exitJiraIssue.nokiaPrio, exitJiraIssue.businessPrio1, exitJiraIssue.plannedSystemRelease, \
			exitJiraIssue.targetPT, exitJiraIssue.reporter, exitJiraIssue.tribeLeader, \
			exitJiraIssue.featureTeam, exitJiraIssue.estimated, exitJiraIssue.problematicReasons, \
			exitJiraIssue.remainEstimate, (exitJiraIssue.estimated - exitJiraIssue.remainEstimate))
		jiraIssuesAfterFBEntry.append(updatedExitJiraIssue)

for entryJiraIssue in fbEntryJiraIssues:
	if getIssueByKey(entryJiraIssue.key, fbExitJiraIssues) == 'Null':
		jiraIssuesDeletedAfterFBEntry.append(entryJiraIssue)

headers = ['Item ID',
         'Key',
         'Summary',
         'Start FB',
         'End FB',
         'Team',
         'FB Commit Status',
         'Status',
         'Assignee',
         'Stretch Reason',
         'Delay Reason',
         'Delay Explanation',
         'Competence Area',
         'Nokia Prio',
         'Business Prio1',
         'Planned System Release',
         'Target PT',
         'Reporter',
         'Tribe Leader',
         'Feature Team',
         'Entry EE',
         'exitEE',
         'contributionHours']

def buildHeaders(worksheet):
	columnIndex = 1
	for header in headers:
	    worksheet.cell(row=1, column=columnIndex).value = header
	    columnIndex += 1

def exportIssueToExcel(worksheet, issue, rowIndex, columnIndex):
        # print(issue.key)
        worksheet.cell(row=rowIndex, column=columnIndex).value    = issue.itemId
        worksheet.cell(row=rowIndex, column=columnIndex+1).value  = issue.key
        worksheet.cell(row=rowIndex, column=columnIndex+2).value  = issue.summary
        worksheet.cell(row=rowIndex, column=columnIndex+3).value  = issue.startFb
        worksheet.cell(row=rowIndex, column=columnIndex+4).value  = issue.endFb
        worksheet.cell(row=rowIndex, column=columnIndex+5).value  = issue.team
        worksheet.cell(row=rowIndex, column=columnIndex+6).value  = issue.fbCommitStatus
        # worksheet.cell(row=rowIndex, column=columnIndex+7).value  = issue.timeSpent
        # worksheet.cell(row=rowIndex, column=columnIndex+8).value  = issue.remainEstimate
        worksheet.cell(row=rowIndex, column=columnIndex+7).value = issue.status
        worksheet.cell(row=rowIndex, column=columnIndex+8).value = issue.assignee
        worksheet.cell(row=rowIndex, column=columnIndex+9).value = issue.stretchReason
        worksheet.cell(row=rowIndex, column=columnIndex+10).value = issue.delayReason
        worksheet.cell(row=rowIndex, column=columnIndex+11).value = issue.delayExplanation
        worksheet.cell(row=rowIndex, column=columnIndex+12).value = issue.competenceArea
        worksheet.cell(row=rowIndex, column=columnIndex+13).value = issue.nokiaPrio
        worksheet.cell(row=rowIndex, column=columnIndex+14).value = issue.businessPrio1
        worksheet.cell(row=rowIndex, column=columnIndex+15).value = issue.plannedSystemRelease
        worksheet.cell(row=rowIndex, column=columnIndex+16).value = issue.targetPT
        worksheet.cell(row=rowIndex, column=columnIndex+17).value = issue.reporter
        worksheet.cell(row=rowIndex, column=columnIndex+18).value = issue.tribeLeader
        worksheet.cell(row=rowIndex, column=columnIndex+19).value = issue.featureTeam
        worksheet.cell(row=rowIndex, column=columnIndex+20).value = issue.entryEE
        worksheet.cell(row=rowIndex, column=columnIndex+21).value = issue.exitEE
        worksheet.cell(row=rowIndex, column=columnIndex+22).value = issue.contributionHours

def exportIssuesToExcel(worksheet, jiraIssues):
	buildHeaders(worksheet)
	rowIndex = 2
	for jiraIssue in jiraIssues:
	    exportIssueToExcel(worksheet, jiraIssue, rowIndex=rowIndex, columnIndex=1)
	    rowIndex += 1
	    # print(rowIndex)

exportIssuesToExcel(committedInFBEntryWs, jiraIssuesInFBEntry)
exportIssuesToExcel(committedAfterFBEntryWs, jiraIssuesAfterFBEntry)
exportIssuesToExcel(deletedAfterFBEntryWs, jiraIssuesDeletedAfterFBEntry)

wb.save(fbCountdownFile)

print('FB countdown file is generated: %s' %fbCountdownFile)








